package graph.autodrawer;

import ernie.database.Database;
import ernie.database.WebMysql;
import graph.Diagram;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import stock.avanza.AvanzaScrape;

public class AutoDrawer implements Runnable {

    private final List<Diagram> dailyDiagrams;
    private final List<Diagram> weeklyDiagrams;
    private final List<Diagram> monthlyDiagrams;
    private final List<Diagram> threemonthlyDiagrams;
    private final List<Diagram> yearlyDiagrams;
    private final Database db;
    private final AvanzaScrape avanza;
    private DrawThread dailyThread;

    public AutoDrawer(AvanzaScrape avanza) {
        this.avanza = avanza;
        dailyDiagrams = new ArrayList<Diagram>();
        weeklyDiagrams = new ArrayList<Diagram>();
        monthlyDiagrams = new ArrayList<Diagram>();
        threemonthlyDiagrams = new ArrayList<Diagram>();
        yearlyDiagrams = new ArrayList<Diagram>();
        db = new WebMysql("127.0.0.1", "avslut");
    }

    public List<String> getDrawedStockNames() {
        List<String> names = new ArrayList<String>();
        for (Diagram d : dailyDiagrams) {
            names.add(d.getTitle());
        }
        return names;
    }

    @Override
    public void run() {
        List<String> stockList = avanza.getMyFollowedStockNames();

        for (String stock : stockList) {
            dailyDiagrams.add(new Diagram(stock, 1024, 500, false));

            Diagram weekDiagram = new Diagram(stock, 1024, 500, false);
            Calendar lastWeek = Calendar.getInstance();
            lastWeek.add(Calendar.DAY_OF_MONTH, -7);
            weekDiagram.setTimePeriod(lastWeek.getTimeInMillis(),
                    System.currentTimeMillis());
            weeklyDiagrams.add(weekDiagram);

            Diagram monthDiagram = new Diagram(stock, 1024, 500, false);
            Calendar lastMonth = Calendar.getInstance();
            lastMonth.add(Calendar.MONTH, -1);
            monthDiagram.setTimePeriod(lastMonth.getTimeInMillis(),
                    System.currentTimeMillis());
            monthlyDiagrams.add(monthDiagram);

            Diagram threemonthDiagram = new Diagram(stock, 1024, 500, false);
            Calendar lastThreeMonth = Calendar.getInstance();
            lastThreeMonth.add(Calendar.MONTH, -3);
            threemonthDiagram.setTimePeriod(lastThreeMonth.getTimeInMillis(),
                    System.currentTimeMillis());
            threemonthlyDiagrams.add(threemonthDiagram);

            Diagram yearlyDiagram = new Diagram(stock, 1024, 500, false);
            Calendar lastYear = Calendar.getInstance();
            lastYear.add(Calendar.MONTH, -8);
            yearlyDiagram.setTimePeriod(lastYear.getTimeInMillis(),
                    System.currentTimeMillis());
            yearlyDiagrams.add(yearlyDiagram);
        }

        dailyThread = new DrawThread(dailyDiagrams, "daily", 30000, db);
        dailyThread.start();
        new DrawThread(weeklyDiagrams, "weekly", 20000, db).start();
        new DrawThread(monthlyDiagrams, "monthly", 30000, db).start();
        new DrawThread(threemonthlyDiagrams, "threemonthly", 100000, db)
                .start();
        new DrawThread(yearlyDiagrams, "yearly", 100000, db).start();
    }

    /**
     * Triggers redraw of a daily stock graph
     * 
     * @param name
     *            the stock name to redraw
     */
    public void triggerDraw(String name) {
        for (Diagram d : dailyDiagrams) {
            if (d.getTitle().equals(name)) {
                d.drawAndSaveDiagram(db, "daily");
                break;
            }
        }
    }

}
